package ace.cmp.sharding.dynamic.datasource.test.core.dao.repository;

import ace.cmp.sharding.dynamic.datasource.core.constant.ShardingDynamicDataSourceConstants;
import ace.cmp.sharding.dynamic.datasource.test.core.dao.entity.CASUser;
import ace.cmp.spring.data.jdbc.querydsl.core.repository.QuerydslJdbcBaseRepository;
import com.baomidou.dynamic.datasource.annotation.DS;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * @author caspar
 * @date 2023/2/13 9:51
 */
@DS(ShardingDynamicDataSourceConstants.DEFAULT_SHARDING_DATA_SOURCE_NAME)
@Repository
public interface CASUserRepository extends QuerydslJdbcBaseRepository<CASUser, String> {
  List<CASUser> findByNameContains(@Param("name") String name);

  @Modifying
  @Query("DELETE FROM cas_user WHERE name LIKE CONCAT('%',:name,'%')")
  Integer deleteAllByNameContains(@Param("name") String name);

  Page<CASUser> findByName(@Param("name") String name, Pageable pageable);

  @Query("SELECT * FROM cas_user WHERE name LIKE CONCAT('%',:name,'%') LIMIT :size OFFSET :offset")
  List<CASUser> findCustomSqlAndPageByName(
      @Param("name") String name, @Param("size") Integer size, @Param("offset") Long offset);
}
